//
//  BillManagerViewController.m
//  SP2P
//
//  Created by 吴建东 on 16/6/7.
//  Copyright © 2016年 EIMS. All rights reserved.
//

#import "BillManagerViewController.h"
#import "MyTermBondsViewController.h"
#import "HistoryBonudsViewController.h"

#define kButtonNum                  2
#define kMyBillButtonTag            11
#define kHistoryBillButtonTag       12
@interface BillManagerViewController ()<UIScrollViewDelegate>
{
    UIScrollView *_scrollView;
    
    UIButton *_investmentButton;
    UIButton *_histroryButton;
    UIView *_buttonView;
    UIView *_buttonLineView;
    
    MyTermBondsViewController *_myBillVC;
    HistoryBonudsViewController *_historyBillVC;
    
}

@end

@implementation BillManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if (iOS7) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    [self initNavBarTitle:@"账单" andLeftItemName:@"返回" andRightItemName:@""];
    
    [self initView];
}

#pragma mark - Action
#pragma 头部导航切换
- (void)headerButtonClick:(UIButton *)button {
    if (button == _investmentButton) {
        [UIView animateWithDuration:0.3 animations:^{
            CGRect frame = _buttonLineView.frame;
            frame.origin.x = 0;
            _buttonLineView.frame = frame;
            _scrollView.contentOffset = CGPointMake(0, 0);
        } completion:^(BOOL finished) {
            [self buttonChange:button];
        }];
    }else {
        [UIView animateWithDuration:0.3 animations:^{
            CGRect frame = _buttonLineView.frame;
            frame.origin.x = MSWIDTH/kButtonNum;
            _buttonLineView.frame = frame;
            _scrollView.contentOffset = CGPointMake(MSWIDTH, 0);
        } completion:^(BOOL finished) {
            [self buttonChange:button];
        }];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == _scrollView) {
        double OriginX = scrollView.contentOffset.x;
        if (OriginX >= 0 && OriginX <= MSWIDTH) {
            CGRect frame = _buttonLineView.frame;
            frame.origin.x = (OriginX/MSWIDTH)*(MSWIDTH/kButtonNum);
            _buttonLineView.frame = frame;
            
            if (OriginX > MSWIDTH/kButtonNum) {
                [self buttonChange:_histroryButton];
            }else {
                [self buttonChange:_investmentButton];
            }
        }
    }
}

- (void)buttonChange:(UIButton *)button {
    if (button == _histroryButton) {
        [_histroryButton setTitleColor:KColor forState:UIControlStateNormal];
        _histroryButton.titleLabel.font = [UIFont boldSystemFontOfSize:14.0];
        [_investmentButton setTitleColor:KBlackColor forState:UIControlStateNormal];
        _investmentButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
    }else {
        [_histroryButton setTitleColor:KBlackColor forState:UIControlStateNormal];
        _histroryButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [_investmentButton setTitleColor:KColor forState:UIControlStateNormal];
        _investmentButton.titleLabel.font = [UIFont boldSystemFontOfSize:14.0];
    }
}

#pragma mark - initView
- (void)initView {
    
    double scrollOriginY = KAdaptNum(45);

    _buttonView = [UIView viewWithFrame:CGRectMake(0, 0, MSWIDTH, scrollOriginY) backgroundColor:[UIColor whiteColor] superview:self.view];
    //_buttonView.hidden = YES;
    
    _investmentButton = [UIButton buttonWithFrame:CGRectMake(0, 0, MSWIDTH/2, scrollOriginY - 1) backgroundColor:nil title:@"理财账单" titleColor:KColor titleFont:14.0 target:self action:@selector(headerButtonClick:) superview:_buttonView];
    
    _histroryButton = [UIButton buttonWithFrame:CGRectMake(MSWIDTH/2, 0, MSWIDTH/2, GetHeight(_investmentButton)) backgroundColor:nil title:@"历史账单" titleColor:KBlackColor titleFont:14.0 target:self action:@selector(headerButtonClick:) superview:_buttonView];
    _histroryButton.titleLabel.font = [UIFont boldSystemFontOfSize:14.0];
    
    UIView *buttonLine = [UIView viewWithFrame:CGRectMake(0, CGRectGetMaxY(_histroryButton.frame), MSWIDTH, _buttonView.frame.size.height - CGRectGetMaxY(_histroryButton.frame)) backgroundColor:KlayerBorder superview:_buttonView];
    
    _buttonLineView = [UIView viewWithFrame:CGRectMake(0, 0, MSWIDTH/2, buttonLine.frame.size.height) backgroundColor:KColor superview:buttonLine];
    
    _scrollView = [UIScrollView scrollViewWithFrame:CGRectMake(0, scrollOriginY, MSWIDTH, MSHIGHT - 64 - scrollOriginY) delegate:self backgroundColor:[UIColor clearColor] superview:self.view];
    _scrollView.contentSize =  CGSizeMake(MSWIDTH * 2, 0);
    _scrollView.pagingEnabled = YES;
    
    _myBillVC = [[MyTermBondsViewController alloc] init];
    _myBillVC.view.frame = CGRectMake(0, 0, MSWIDTH, _scrollView.frame.size.height);
    _myBillVC.tableView.frame =CGRectMake(0, 0, MSWIDTH, _scrollView.frame.size.height);
    [_scrollView addSubview:_myBillVC.view];
    
    _historyBillVC = [[HistoryBonudsViewController alloc] init];
    _historyBillVC.view.frame = CGRectMake(MSWIDTH, 0, MSWIDTH, _scrollView.frame.size.height);
    _historyBillVC.tableView.frame =CGRectMake(0, 0, MSWIDTH, _scrollView.frame.size.height);
    [_scrollView addSubview:_historyBillVC.view];

   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
